package com.elitel.common.businessJdbc;

import java.util.Map;

import com.elitel.common.utils.XCommonUtils;
import com.elitel.frame.main.entity.constant.DictionaryConstant;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class BusinessUpdatePreparedStatement extends AbstractBusinessPreparedStatement{
    private String tableName;
    private Map<String, Object> updateColumnMap;
    private Map<String, Object> conditionColumnMap;
    private Map<String, String> fieldTypeMap;

    public static Builder builder(){
        return new Builder(DictionaryConstant.DatabaseType_MYSQL);
    }

    public static Builder builder(String databaseType){
        return new Builder(databaseType);
    }

    @Override
    public String getPrecompileSql(){
        StringBuilder sb = new StringBuilder();
        sb.append(" update ")
            .append(getTableName())
            .append(" set ")
            .append(updateColumnSectionSql());

        if(existsWhereCondition())
            sb.append(whereConditionSectionSql());

        log.info("update precompile sql: " + sb.toString());
        return sb.toString();
    }

    public String updateColumnSectionSql(){
        StringBuilder sb = new StringBuilder();
        updateColumnMap.forEach((columnName, strColumnValue) -> {
                if(sb.length() == 0){
                    sb.append(columnName).append(" = ").append("?");
                    return;
                }
                sb.append(", ").append(columnName).append(" = ").append("?");
            });
        return sb.toString();
    }

    public boolean existsWhereCondition(){
        return XCommonUtils.isNotEmpty(conditionColumnMap);
    }

    public String whereConditionSectionSql(){
        StringBuilder sb = new StringBuilder();
        conditionColumnMap.forEach((columnName, strColumnValue) -> {
                if(sb.length() == 0){
                    sb.append(" where ").append(columnName).append(" = ").append("?");
                    return;
                }
                sb.append(", ").append(columnName).append(" = ").append("?");
            });
        return sb.toString();
    }

    public static class Builder{
        private String serviceGuid;
        private String databaseType;
        private String tableName;
        private Map<String, Object> updateColumnMap;
        private Map<String, Object> conditionColumnMap;
        private Map<String, String> fieldTypeMap;

        public Builder(String databaseType){
            this.databaseType = databaseType;
        }

        public Builder serviceGuid(String serviceGuid){
            this.serviceGuid = serviceGuid;
            return this;
        }

        public Builder tableName(String tableName){
            this.tableName = tableName;
            return this;
        }

        public Builder updateColumnMap(Map<String, Object> updateColumnMap){
            this.updateColumnMap = updateColumnMap;
            return this;
        }

        public Builder conditionColumnMap(Map<String, Object> conditionColumnMap){
            this.conditionColumnMap = conditionColumnMap;
            return this;
        }

        public Builder fieldTypeMap(Map<String, String> fieldTypeMap){
            this.fieldTypeMap = fieldTypeMap;
            return this;
        }

        public BusinessUpdatePreparedStatement build(){
            BusinessUpdatePreparedStatement bps = new BusinessUpdatePreparedStatement();
            bps.setDatabaseType(databaseType);
            bps.setServiceGuid(serviceGuid);
            bps.setTableName(tableName);
            bps.setUpdateColumnMap(updateColumnMap);
            bps.setConditionColumnMap(conditionColumnMap);
            bps.setFieldTypeMap(fieldTypeMap);
            return bps;
        }

    }

}
